<?php

namespace common\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\ApiItem;

/**
 * ApiItemSearch represents the model behind the search form about `common\models\ApiItem`.
 */
class ApiItemSearch extends ApiItem
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'api_type_id', 'api_limit', 'version', 'is_auth', 'status', 'created_at', 'updated_at'], 'integer'],
            [['api_name', 'api_params', 'info','api_route'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
// bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ApiItem::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'api_type_id' => $this->api_type_id,
            'api_limit' => $this->api_limit,
            'api_route' => $this->api_route,
            'version' => $this->version,
            'is_auth' => $this->is_auth,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'api_name', $this->api_name])
            ->andFilterWhere(['like', 'api_params', $this->api_params])
            ->andFilterWhere(['like', 'info', $this->info]);

        return $dataProvider;
    }
}